package util

import (
	"gitee.com/chunanyong/zorm"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"go.uber.org/zap"
	"zorm-learn/app/auth/model"
)

type MainContext struct {
	RouterGroup      *gin.RouterGroup
	DB               *zorm.DBDao
	DM               *zorm.DBDao
	OriginalApisHost string
	Logger           *zap.Logger
	Sessions         *Sessions
	DataSources      []*zorm.DBDao
	RetryTime        int
}
type Sessions struct {
	UserTokens map[int]string
	Session    map[string]interface{}
}

func (s *Sessions) CreateSession(u *model.UsersStruct) string {
	if s.UserTokens[u.Id] != "" {
		return s.UserTokens[u.Id]
	} else {
		token := uuid.New().String()
		s.UserTokens[u.Id] = token
		s.Session[token] = u
		return token
	}
}
func (s *Sessions) GetLoginUser(token string) *model.UsersStruct {
	i := s.Session[token]
	if i == nil {
		return nil
	}
	usersStruct := i.(*model.UsersStruct)
	return usersStruct
}

var MainCtx *MainContext

func init() {
	MainCtx = &MainContext{Sessions: &Sessions{
		UserTokens: map[int]string{},
		Session:    map[string]interface{}{},
	}}
}
